﻿<%@ Page Language="C#" %>
<%@ Import Namespace="BLF.WWW_WWW.Files" %>
<%@ Import Namespace="Model.WWW_WWW.files" %>

<script runat="server">
    protected Int32 val;
    protected String scrpit1 = @"<script type=""text/javascript"">parent.window.SaveFinish('{0}');</" + "script>";
    protected override void OnLoad(EventArgs e)
    {
        if (!string.IsNullOrEmpty(Request["LongxinUploadGUID"]))
        {
            Application.Remove("_Error_" + Request["LongxinUploadGUID"]);
            Application.Remove("_UploadGUID_" + Request["LongxinUploadGUID"]);
        }
        
        if (!Page.IsPostBack)
        {
            string FilePath = Server.MapPath(System.Configuration.ConfigurationManager.AppSettings["UploadFileFolader"]);
            String title = "";
            String desc = "";
            String learLang = "";
            String baseLang = "";
            String level = "";
            String tags = "";
            String txtTags = "";
            String fileDocs = "";
            String fileFormat = "";
            if (Request.Form["txtTitle"] != null)
            {
                title = Request.Form["txtTitle"].ToString();
            }
            if (Request.Form["txtDesc"] != null)
            {
                desc = Request.Form["txtDesc"].ToString();
            }
            if (Request.Form["learLang"] != null)
            {
                learLang = Request.Form["learLang"].ToString();
            }
            if (Request.Form["baseLang"] != null)
            {
                baseLang = Request.Form["baseLang"].ToString();
            }
            if (Request.Form["level"] != null)
            {
                level = Request.Form["level"].ToString();
            }
            if (Request.Form["tags"] != null)
            {
                tags = Request.Form["tags"].ToString();
            }
            if (Request.Form["txtTags"] != null)
            {
                txtTags = Request.Form["txtTags"].ToString();
            }
            if (Request.Form["fileDocs"] != null)
            {
                fileDocs = Request.Form["fileDocs"].ToString();
            }
            try
            {
                //如果没有上传成功
                if (string.IsNullOrEmpty(Request.Form["fileDocs"]))
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "dd", scrpit1.Replace("{0}", "ML"));
                    return;
                }
                //如果用户没有登录
                if (HttpContext.Current.User.Identity.Name == "")
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "dd", scrpit1.Replace("{0}", "-1"));
                    return; 
                }
                
                String[] fileArray = fileDocs.Split(';');
                if (fileArray.Length > 2)
                {
                    fileFormat = fileArray[0].Split(':')[1];
                    fileDocs = fileArray[2].Split('\"')[1];

                    if (System.IO.Path.GetExtension(fileDocs.Trim()).ToLower().TrimStart(new char[] { '.' }) == "htm")
                    {
                        System.IO.File.Copy(System.IO.Path.Combine(FilePath, fileDocs), System.IO.Path.Combine(FilePath, fileDocs) + "l");
                        System.IO.File.Delete(System.IO.Path.Combine(FilePath, fileDocs));
                        fileDocs = fileDocs + "l";
                    }
                    FileDocModel model = new FileDocModel();
                    model.BaseLanguage = baseLang;
                    model.CreateDate = DateTime.Now;
                    model.Desc = desc;
                    model.DocID = Guid.NewGuid();
                    model.DocUrlPath = fileDocs;
                    model.DownLoads = 0;
                    model.FileFormat = fileFormat;
                    model.LanguageLearn = learLang;
                    model.Level = level;
                    model.MemberNo = HttpContext.Current.User.Identity.Name;
                    model.ScribdDocID = 0;
                    model.ScribdKey = "";
                    model.ScribdState = 0;
                    model.Title = title;
                    model.Views = 0;

                    string[] tagList = tags.Split(',');
                    string[] txtTagList = txtTags.Split(' ');
                    string[] totalTagList = new string[tagList.Length + txtTagList.Length];
                    tagList.CopyTo(totalTagList,0);
                    txtTagList.CopyTo(totalTagList,tagList.Length);
                    
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "dd", scrpit1.Replace("{0}", Files.AddFile(model, totalTagList).ToString()));
                }
                else
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "dd", scrpit1.Replace("{0}", "-1"));
                }
            }
            catch
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "dd", scrpit1.Replace("{0}", "-1"));
            }
        }
    }
</script>
<html>
<head>
<title></title>
</head>
<body>
<form id="form1" runat="server">
</form>
</body>
</html>